package com.meraklisina.latex ;

/**
 *
 * @author lacertadv7
 * In this version this class is not used.
 */
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JToolBar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlHandler
{

    private Icon icon ;

    private LatexButton[ ] buttons ;

    public JToolBar[ ] toolbars ;

    /*public static void main(String args[]) throws ParserConfigurationException, SAXException, XPathExpressionException {

    //  LatexButton[] buttons;
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    JToolBar toolbar = new JToolBar();

    try {
    readMenu("alltoolbars.conf","//toolbar/name/");
    } catch (IOException ex) {
    Logger.getLogger(XmlHandler.class.getName()).log(Level.SEVERE, null, ex);
    }

    }*/
    public void XmlHandler( )
    {

    }

    private String normalizePath( String path )
    {
        //String returnpath=path.replace("::", File.pathSeparator);
        //String returnpath = path.replace("::", System.getProperty("file.seperator"));
        // System.out.print("seperator is " + System.getProperty("file.separator"));
        String returnpath = path.replace( "::", "/" ) ;
        return returnpath ;
    }

    public JToolBar[ ] createToolBars( String xmlPath, JToolBar panel, ActionListener actionListener, JMenu jmenu )
    {

        File docFile = new File( xmlPath ) ;
        System.out.println( docFile.getAbsolutePath( ) ) ;

        Document doc = null ;
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance( ) ;
            dbf.setIgnoringElementContentWhitespace( true ) ;
            DocumentBuilder db = dbf.newDocumentBuilder( ) ;

            doc = db.parse( docFile ) ;
        }
        catch( java.io.IOException e )
        {
            System.out.println( "Can't find the file" ) ;
        }
        catch( Exception e )
        {
            System.out.print( "Problem parsing the file." ) ;
        }
        Element root = doc.getDocumentElement( ) ;
        NodeList children = root.getChildNodes( ) ;
        JToolBar[ ] menus = new JToolBar[ children.getLength( ) ] ;

        this.toolbars = new JToolBar[ children.getLength( ) ] ;

        int i = 0 ;
        int toolbarCount = 0 ;

        for( Node child = root.getFirstChild( ); child != null; child = child.getNextSibling( ) )
        {
            String toolbarName = "" ;
            String toolbarPath = "" ;

            if( child.getNodeType( ) == Node.ELEMENT_NODE )
            {
                //System.out.println("name: "+i+" : "+child.getFirstChild().getNodeName());
                NodeList subNodes = child.getChildNodes( ) ;

                for( int j = 0; j < subNodes.getLength( ); j++ )
                {
                    Node subNodeItem = subNodes.item( j ) ;
                    if( subNodeItem.getNodeType( ) == Node.ELEMENT_NODE )
                    {
                        if( subNodeItem.getNodeName( ) == "name" )
                        {
                            toolbarName = subNodeItem.getFirstChild( ).getNodeValue( ) ;
                        }
                        else if( subNodeItem.getNodeName( ) == "path" )
                        {
                            toolbarPath = subNodeItem.getFirstChild( ).getNodeValue( ) ;
                        }
                    }
                }

                LatexButton[ ] xmlButtons ;
                xmlButtons = this.readXml( this.normalizePath( toolbarPath ), actionListener ) ;

                JToolBar xmlToolbar = new JToolBar( ) ;
                this.toolbars[ toolbarCount ] = xmlToolbar ;
                toolbarCount++ ;

                for( int k = 0; k < xmlButtons.length; k++ )
                {
                    if( xmlButtons[ k ] != null )
                    {
                        xmlToolbar.add( xmlButtons[ k ] ) ;
                    }
                }
                //System.out.println("startx: "+startX+" : "+toolbarCount);
                xmlToolbar.setSize( 300, 25 ) ;
                xmlToolbar.setRollover( true ) ;
                xmlToolbar.setAlignmentX( 5 ) ;
                //xmlToolbar.setLocation(startX, 5);
                xmlToolbar.getWidth( ) ;
                xmlToolbar.setName( toolbarName ) ;
                panel.add( toolbarName, xmlToolbar ) ;
                LayoutManager toolbarPanelLayout = panel.getLayout( ) ;
                toolbarPanelLayout.addLayoutComponent( toolbarName, xmlToolbar ) ;
                panel.validate( ) ;

                JCheckBoxMenuItem checkbox = new JCheckBoxMenuItem( ) ;
                checkbox.setSelected( true ) ;
                checkbox.setText( toolbarName ) ;
                checkbox.setName( toolbarName ) ;

                checkboxActionListener checkboxListener = new checkboxActionListener( ) ;

                checkbox.addActionListener( checkboxListener ) ;
                jmenu.add( checkbox ) ;
                //System.out.println("name: "+toolbarName+" path: "+toolbarPath);
            }
            i++ ;
        }
        return menus ;
    }

    public class checkboxActionListener implements ActionListener
    {

        public void actionPerformed( ActionEvent evt )
        {
            JCheckBoxMenuItem checkbox = ( JCheckBoxMenuItem )evt.getSource( ) ;

            int count = XmlHandler.this.toolbars.length ;
            for( int i = 0; i < count; i++ )
            {
                // System.out.println("..."+i);
                JToolBar toolbar = XmlHandler.this.toolbars[ i ] ;

                if( checkbox.getName( ) == toolbar.getName( ) )
                {
                    toolbar.setVisible( checkbox.isSelected( ) ) ;

                    break ;
                }

            }

            //Object ob = evt.getSource();
            //LatexButton lb = (LatexButton) evt.getSource();
            //addLatexCommand(lb.getPrefix(), lb.getPostfix());
        }
    }

    public LatexButton[ ] readXml( String xmlPath, ActionListener actionListener )
    {

        //   LatexButton[] buttons; //temp button
        File docFile = new File( xmlPath ) ;

        Document doc = null ;
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance( ) ;
            DocumentBuilder db = dbf.newDocumentBuilder( ) ;
            doc = db.parse( docFile ) ;
        }
        catch( java.io.IOException e )
        {
            System.out.println( "Can't find the file" ) ;
        }
        catch( Exception e )
        {
            System.out.print( "Problem parsing the file." ) ;
        }
        Element root = doc.getDocumentElement( ) ;
        //System.out.println("The root element is " + root.getNodeName() + ".\n");

        NodeList children = root.getChildNodes( ) ;

        this.buttons = new LatexButton[ children.getLength( ) ] ;
        int i = 0 ;
        for( Node child = root.getFirstChild( ); child != null; child = child.getNextSibling( ) )
        {

            this.buttons[ i ] = new LatexButton( ) ;
            for( Node grandChild = child.getFirstChild( ); grandChild != null; grandChild = grandChild.getNextSibling( ) )
            {
                if( grandChild.getNodeType( ) == Node.TEXT_NODE )
                {
                    // System.out.println("Text: " + grandChild.getNodeValue());
                }
                else if( grandChild.getNodeType( ) == Node.ELEMENT_NODE )
                {

                    if( grandChild.getNodeName( ) == "name" )
                    {
                        this.buttons[ i ].addActionListener( actionListener ) ;
                        this.buttons[ i ].setName( grandChild.getFirstChild( ).getNodeValue( ) ) ;
                        //buttons[i].setText(grandChild.getFirstChild().getNodeValue());
                    }
                    else if( grandChild.getNodeName( ) == "type" )
                    {
                        this.buttons[ i ].setType( grandChild.getFirstChild( ).getNodeValue( ) ) ;
                    }
                    else if( grandChild.getNodeName( ) == "tooltip" )
                    {
                        this.buttons[ i ].setTooltip( grandChild.getFirstChild( ).getNodeValue( ) ) ;
                    }
                    else if( grandChild.getNodeName( ) == "prefix" )
                    {

                        try
                        {
                            this.buttons[ i ].setPrefix( grandChild.getFirstChild( ).getNodeValue( ) ) ;
                        }
                        catch( java.lang.NullPointerException e )
                        {
                        }

                    }
                    else if( grandChild.getNodeName( ) == "postfix" )
                    {
                        //buttons[i].setPostfix(grandChild.getFirstChild().getNodeValue());
                        try
                        {
                            this.buttons[ i ].setPostfix( grandChild.getFirstChild( ).getNodeValue( ) ) ;
                        }
                        catch( java.lang.NullPointerException e )
                        {
                        }
                    }
                    else if( grandChild.getNodeName( ) == "icon" )
                    {
                        this.buttons[ i++ ].setIconString( this.normalizePath( grandChild.getFirstChild( ).getNodeValue( ) ) ) ;
                    }
                    else
                    {
                        System.out.println( "Parsing " + grandChild.getFirstChild( ).getNodeValue( ) ) ;
                    }
                }
            }
            this.buttons[ i ] = null ;
        }
        return this.buttons ;
    }

    private static void populateToolbar( )
    {
    }
}
